
$(document).ready(function() {

	// MAINPAGE.jsp select value
	$('#MainPageSelectField').change(function() {
		if ($('#MainPageSelectField').val() == 'all_types') {
			// show all_types,
		//	alert("all_types");
			// show
			// show
			// show

			$('#secondaryInput').hide();
		}
		if ($('#MainPageSelectField').val() == 'person') {

			// TODO
		//	alert("person");

		}

		if ($('#MainPageSelectField').val() == 'enterprise') {
			// TODO
		//	alert("enterprise");
		}

		if ($('#MainPageSelectField').val() == 'customer') {
			// TODO
		//	alert("customer");
		}

		if ($('#MainPageSelectField').val() == 'employee') {
			// TODO
		//	alert("employee");

		}

	});

	// MAINPAGE.jsp select value end;

	// MODIFYPERSON.JSP START
	$("#addresses").hide();
	$("#contacts").hide();

	$("#display_contacts").click(function() {

		$("#contacts").show();
		$("#addresses").hide();
		$("#show_modify_person").hide();

	});

	$("#display_modify_person").click(function() {

		$("#contacts").hide();
		$("#addresses").hide();
		$("#show_modify_person").show();

	});

	// DISPLAY _ADRESSES
	$("#display_addresses").click(function() {

		$("#addresses").show();
		$("#contacts").hide();
		$("#show_modify_person").hide();
	});

});

// MODIFYPERSON.JSP END

function search_people() {

	var start = new Date();
	var tm = start.getTime();
//	alert("uus115531");

	var firstname = document.getElementById("firstname");

	var lastname = document.getElementById("lastname");

	// realiseerida

	var clients_interest = document.getElementById("clients_interest");
	var discount = document.getElementById("discount");

	var country = document.getElementById("country");
	var county = document.getElementById("county");
	var town_village = document.getElementById("town_village");
	var street_aadress = document.getElementById("street_aadress");
	var zipcode = document.getElementById("zipcode");
	var contact = document.getElementById("contact");

	// tegemata
	// Subjekti t��bi valik

	var url = "http://localhost:8080/JSP_Servlet_Subjekt/tm=" + tm;

	if (firstname != "") {
		url += "&firstname=" + firstname.value;
	}

	if (lastname != "") {
		url += "&lastname=" + lastname.value;
	}

	//alert(url);

	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

			var person_answer = JSON.parse(xmlhttp.responseText);

			htmldata = '<table><tr><td>Eesnimi</td><td>perenimi</td></tr>'

			for ( var i = 0; i < person_answer.length; i++) {

				htmldata += '<tr><td>' + person_answer[i].first_name + '</td>'
				htmldata += '<td>' + person_answer[i].last_name + '</td></tr>'

			}

			htmldata += "</table>"
			$('#vormi_vastus').html(htmldata);

		}
	}

	xmlhttp.open("GET", url, true);
	xmlhttp.send();
}

// REMOVE_ROLE

function remove_role(employeeid) {
	var start = new Date();
	var tm = start.getTime();

	selectedvalue = $('#employee_role').val();
	// vaadata üle
	

	//var url = "http://localhost:8080/JSP_Servlet_Subjekt/DeleteRoleService?employeeid="
	//+ employeeid + '&selectvalue=' + selectedvalue;
	
	
	var url = "http://imbi.ld.ttu.ee:7500/t103755_servlet_subjekt/DeleteRoleService?employeeid="
			+ employeeid + '&selectvalue=' + selectedvalue;
	
	// '&tm='+new Date().getTime()
	// alert( selectedvalue);
	//	
	// selectedvalue.find('option[value=kliendihaldur]').remove();
	//	

	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

			// var person_answer = JSON.parse(xmlhttp.responseText);

//			htmldata = 'success'

			/*
			 * htmldata = '<table><tr><td>Eesnimi</td><td>perenimi</td></tr>'
			 * 
			 * for(var i = 0; i < person_answer.length; i++){
			 * 
			 * 
			 * 
			 * htmldata +='<tr><td>'+person_answer[i].first_name+'</td>'
			 * htmldata +='<td>'+person_answer[i].last_name+'</td></tr>'
			 *  }
			 * 
			 * htmldata += "</table>"
			 */
//			$('#remove_role_message').html(htmldata);

		}
	}

	xmlhttp.open("POST", url, true);
	xmlhttp.send();
}
